import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.sql.*;
/*
 * Created by JFormDesigner on Sun Dec 08 18:28:23 CST 2024
 */



/**
 * @author jjchen
 */
public class user_Home extends JFrame {
    public user_Home() {
        initComponents();
    }

    private void button8MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Book_Borrowing book = new Book_Borrowing();
        book.setVisible(true);
    }

    private void button4MouseClicked(MouseEvent e) {
        this.setVisible(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        findBook_user fou = new findBook_user();
        fou.setVisible(true);
    }

    private void button7MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void modifybutton(MouseEvent e) {
        modify_information modify_information = new modify_information();
        modify_information.setVisible(true);
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void button3MouseClicked(MouseEvent e) {
        Passwordchange_user passwordchangeUser = new Passwordchange_user();
        passwordchangeUser.setVisible(true);
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void button6MouseClicked(MouseEvent e) {
        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String password = "root";
        String sql = "SELECT * FROM book_returning_record_mix WHERE user_ID=? and if_return = 1";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, Lgoin_user.userid);
            ResultSet rs = pstmt.executeQuery();

            StringBuilder result = new StringBuilder();
            while (rs.next()) {
                result.append("图书编号: ").append(rs.getString("book_id")).append("\n")
                        .append("书名: ").append(rs.getString("book_title")).append("\n")
                        .append("用户ID: ").append(rs.getString("user_ID")).append("\n")
                        .append("归还日期: ").append(rs.getString("return_time")).append("\n")
                        .append("\n");
            }

            showResultsInGUI(result.toString());

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void button5MouseClicked(MouseEvent e) {
        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String password = "root";
        String sql = "SELECT * FROM book_returning_record_mix WHERE user_ID=?";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, Lgoin_user.userid);
            ResultSet rs = pstmt.executeQuery();
            StringBuilder result = new StringBuilder();

            while (rs.next()) {
                result.append("图书编号: ").append(rs.getString("book_id")).append("\n")
                        .append("书名: ").append(rs.getString("book_title")).append("\n")
                        .append("用户id: ").append(rs.getString("user_ID")).append("\n")
                        .append("借阅时间: ").append(rs.getString("borrow_time")).append("\n")
                        .append("\n");
            }
            showResultsInGUI(result.toString());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void showResultsInGUI(String results) {
        Borrowing_Select borrowingSelect = new Borrowing_Select();
        String htmlResults = results.replace("\n", "<br>");
        borrowingSelect.label1.setText("<html>" + htmlResults + "</html>");
        borrowingSelect.pack();
        borrowingSelect.setVisible(true);
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        Lgoin_user login = new Lgoin_user();
        login.setVisible(true);
    }

    private void button9MouseClicked(MouseEvent e) {
        this.dispose();
        Book_Returning book1 = new Book_Returning();
        book1.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button1 = new JButton();
        label2 = new JLabel();
        label1 = new JLabel();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button8 = new JButton();
        button9 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[74]" +
            "[54]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- button1 ----
        button1.setText("\u8fd4\u56de");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1, "cell 6 3");

        //---- label2 ----
        label2.setText("\u7528\u6237\u4e3b\u9875");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getStyle() | Font.BOLD, label2.getFont().getSize() + 12f));
        contentPane.add(label2, "cell 11 3");

        //---- label1 ----
        label1.setText("  \u529f\u80fd\u5217\u8868\uff1a");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 6f));
        contentPane.add(label1, "cell 6 5");

        //---- button2 ----
        button2.setText("\u4fee\u6539\u4e2a\u4eba\u4fe1\u606f");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                modifybutton(e);
            }
        });
        contentPane.add(button2, "cell 10 7");

        //---- button3 ----
        button3.setText("\u4fee\u6539\u5bc6\u7801");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3, "cell 12 7");

        //---- button4 ----
        button4.setText("\u67e5\u8be2\u56fe\u4e66");
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button4MouseClicked(e);
            }
        });
        contentPane.add(button4, "cell 10 11");

        //---- button8 ----
        button8.setText("\u501f\u9605\u56fe\u4e66");
        button8.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button8MouseClicked(e);
            }
        });
        contentPane.add(button8, "cell 12 11");

        //---- button9 ----
        button9.setText("\u5f52\u8fd8\u56fe\u4e66");
        button9.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button9MouseClicked(e);
            }
        });
        contentPane.add(button9, "cell 10 16");

        //---- button5 ----
        button5.setText("\u67e5\u770b\u501f\u9605\u8bb0\u5f55");
        button5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button5MouseClicked(e);
            }
        });
        contentPane.add(button5, "cell 12 16");

        //---- button6 ----
        button6.setText("\u67e5\u770b\u8fd8\u4e66\u8bb0\u5f55");
        button6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button6MouseClicked(e);
            }
        });
        contentPane.add(button6, "cell 10 21");

        //---- button7 ----
        button7.setText("\u9000\u51fa\u7cfb\u7edf");
        button7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button7MouseClicked(e);
            }
        });
        contentPane.add(button7, "cell 12 21");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        user_Home frame = new user_Home();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button1;
    private JLabel label2;
    private JLabel label1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button8;
    private JButton button9;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
